package com.legsem.jrhosync.ws.rs;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.legsem.jhosync.dto.ClientLogin;

public class ClientResetResourceITCase extends AbstractResourceITCase {

    public void testShouldResetClient() throws Exception {

        Pattern pattern1 = Pattern
                .compile("\"Customer\":\\{\"poll_interval\":3\\}");
        Pattern pattern2 = Pattern
                .compile("\"Appointment\":\\{\"poll_interval\":300\\}");

        ClientLogin clientLogin = new ClientLogin();
        clientLogin.setLogin("tartempion");
        clientLogin.setPassword("");
        postJSONRequest("/clientlogin", clientLogin);

        byte[] response = getJSONRequest("/clientcreate");
        String readableResponse = new String(response, "UTF-8");
        String client_id = getClientId(readableResponse);

        response = getJSONRequest("/clientreset" + "?client_id="
                + client_id);
        readableResponse = new String(response, "UTF-8");

        Matcher matcher = pattern1.matcher(readableResponse);
        assertTrue(matcher.find());
        matcher = pattern2.matcher(readableResponse);
        assertTrue(matcher.find());
    }

    public String getClientId(String content) throws IOException {
        Pattern pattern = Pattern.compile("\\{\"client_id\":\"(\\w+)\"\\}");
        Matcher matcher = pattern.matcher(content);
        matcher.find();
        return matcher.group(1);
    }

}
